<!-- 编辑弹窗 -->
<template>
  <ele-modal :form="true" :destroy-on-close="true" :width="460" v-model="visible"
    :title="isUpdate ? '修改员工违规分析' : '添加员工违规分析'">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px" @submit.prevent="">
      <el-form-item label="员工" prop="userId">
        <user-select v-model="form.userId" placeholder="请选择员工" />
      </el-form-item>
      <el-form-item label="所属组织" prop="deptId">
        <dept-select v-model="form.deptId" placeholder="请选择所属组织" />
      </el-form-item>
      <el-form-item label="违规次数" prop="avgScore">
        <el-input clearable v-model="form.avgScore" placeholder="请输入违规次数" />
      </el-form-item>
      <!-- <el-form-item label="创建人" prop="createdBy">
        <el-input clearable v-model="form.createdBy" placeholder="请输入创建人" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdTime">
        <el-date-picker v-model="form.createdTime" value-format="YYYY-MM-DD" placeholder="请选择创建时间" class="ele-fluid" />
      </el-form-item>
      <el-form-item label="修改人" prop="updateBy">
        <el-input clearable v-model="form.updateBy" placeholder="请输入修改人" />
      </el-form-item>
      <el-form-item label="修改时间" prop="updateTime">
        <el-date-picker v-model="form.updateTime" value-format="YYYY-MM-DD" placeholder="请选择修改时间" class="ele-fluid" />
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
import { ref, reactive, watch } from 'vue';
import { EleMessage } from 'ele-admin-plus';
import { useFormData } from '@/utils/use-form-data';
import { addEtpEmployeeViolationAnalysis, updateEtpEmployeeViolationAnalysis } from '@/api/etp/etpEmployeeViolationAnalysis';
import DeptSelect from '@/views/system/dept/components/dept-select.vue';
import UserSelect from '@/views/system/user/components/user-select.vue';

const props = defineProps({
  /** 修改回显的数据 */
  data: Object
});

const emit = defineEmits(['done']);

/** 弹窗是否打开 */
const visible = defineModel({ type: Boolean });

/** 是否是修改 */
const isUpdate = ref(false);

/** 提交状态 */
const loading = ref(false);

/** 表单实例 */
const formRef = ref(null);

/** 表单数据 */
const [form, resetFields, assignFields] = useFormData({
  id: void 0,
  userId: void 0,
  deptId: void 0,
  avgScore: void 0,
  createdBy: '',
  createdTime: '',
  updateBy: '',
  updateTime: ''
});

/** 表单验证规则 */
const rules = reactive({
});

/** 关闭弹窗 */
const handleCancel = () => {
  visible.value = false;
};

/** 保存编辑 */
const save = () => {
  formRef.value?.validate?.((valid) => {
    if (!valid) {
      return;
    }
    loading.value = true;
    const saveOrUpdate = isUpdate.value ? updateEtpEmployeeViolationAnalysis : addEtpEmployeeViolationAnalysis;
    saveOrUpdate(form)
      .then((msg) => {
        loading.value = false;
        EleMessage.success({ message: msg, plain: true });
        handleCancel();
        emit('done');
      })
      .catch((e) => {
        loading.value = false;
        EleMessage.error({ message: e.message, plain: true });
      });
  });
};

/** 监听弹窗打开 */
watch(visible, () => {
  if (visible.value) {
    if (props.data) {
      assignFields(props.data);
      isUpdate.value = true;
    } else {
      resetFields();
      isUpdate.value = false;
    }
  }
});
</script>
